// pages/distribut/order_list.js
var app = getApp();
var  server = require("../../../utils/server.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentData: 0,
    status:['0,4','1,2','3'],
    list:'',
    page:1,
    count:0,
    limit:0,
    pagestatus:true,
  },

  //选项卡
  //获取当前滑块的index
  bindchange: function (e) { const that = this; that.setData({ currentData: e.detail.current }) },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;
    that.setData({
      page:1,
    })
    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {
      that.setData({ currentData: e.target.dataset.current })
    }
    // this.select(e.target.dataset.current)
    var status = that.data.status;
    var status = status[that.data.currentData];
    that.list(status);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var status = that.data.status;
    var status = status[that.data.currentData];
    that.list(status);
  },

  list:function(status){
    var that = this;
    server.postJSON("/Distribut/order_list", { userid: app.globalData.userInfo.user_id, is_distribut: app.globalData.userInfo.is_distribut,status:status,page:that.data.page,},function(e){
      if(e.data.code == 1){
          that.setData({
            list:e.data.data,
            count:e.data.count,
            limit:e.data.limit,
          })
      }
    })
  },

  listpage: function (status) {
    var that = this;
    var userinfo = app.globalData.userInfo;
    server.postJSON("/Distribut/order_list", { userid: app.globalData.userInfo.user_id, is_distribut: app.globalData.userInfo.is_distribut, status: status, page: that.data.page,}, function (res) {
      if (res.data.code == 1) {
        var list = that.data.list;
        for (var i = 0; i < res.data.data.length; i++) {
          list.push(res.data.data[i])
        }
        // list = Object.assign(list,res.data.log);
        that.setData({
          list: list,
          pagestatus: true,
        })
        console.log(list)
      }else{
        wx.showToast({
          title: e.data.msg,
          icon:'none',
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.pagestatus) {
      return;
    }
    var page = this.data.page;
    var count = this.data.count;
    var limit = this.data.limit;
    var limitpage = Math.ceil(count / limit);
    if (page == limitpage) {
      wx.showLoading({
        title: '数据加载完毕',
      })
      setTimeout(function () {
        wx.hideLoading();
      }, 2000)
      return;
    }
    this.setData({
      pagestatus: false,
    })
    page++;
    this.setData({
      page: page,
    })
    var type = this.data.status;
    var radio = this.data.currentData;
    this.listpage(type[radio]);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})